//
//  LBDingzhiVC.m
//  V-Learn
//
//  Created by Apple on 2018/12/31.
//  Copyright © 2018 yxk. All rights reserved.
//

#import "LBDingzhiVC.h"
#import "LBZaiLineCell.h"
#import "BRStringPickerView.h"
#import "WKOnlinePayVC.h"
#import "NSString+Category.h"
#import "WKOnlinePingguResultModel.h"
#import "LBVipEvaluationResultVC.h"
@interface LBDingzhiVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *ageLab;
@property (nonatomic, strong) UILabel *monthLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) NSArray *titleArr;

@property (nonatomic, strong) NSString *ageString;
@property (nonatomic, strong) NSString *monthString;
@property (nonatomic, strong) NSString *timeLongString;
@property (nonatomic, strong) NSString *tel;

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) VKPersonInfoModel *personInfoModel;
@end

@implementation LBDingzhiVC
- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"孩子年龄",@"孩子月份",@"每天学习时长",@"手机号码"];
    }
    return _titleArr;
}
- (UILabel *)ageLab{
    if (!_ageLab) {
        _ageLab = [UILabel labelWithText:@"" andTextColor:rgb(51,51,51) andName:nil andFontSize:14];
    }
    return _ageLab;
}
- (UILabel *)monthLab{
    if (!_monthLab) {
        _monthLab = [UILabel labelWithText:@"" andTextColor:rgb(51,51,51) andName:nil andFontSize:14];
    }
    return _monthLab;
}
- (UILabel *)timeLab{
    if (!_timeLab) {
        _timeLab = [UILabel labelWithText:@"" andTextColor:rgb(51,51,51) andName:nil andFontSize:14];
    }
    return _timeLab;
}


- (void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.title = @"专属定制";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH-LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = rgb(242,242,242);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    UIView *footerView = [UIView new];
    footerView.backgroundColor = rgb(242,242,242);
    
    self.tableView.tableFooterView = footerView;
    footerView.frame = CGRectMake(0, 0, ScreenW, 300);
    [self.view addSubview:self.tableView];
    
    UIButton *productButton = [[UIButton alloc] init];
    [self.view addSubview:productButton];
    productButton.layer.cornerRadius = 22;
    productButton.layer.masksToBounds = YES;
    [productButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(-90);
    }];
    [productButton setTitle:@"生成规划" forState:UIControlStateNormal];
    [productButton setTitleColor:rgb(255,255,255) forState:UIControlStateNormal];
    productButton.titleLabel.font = LBFont(18);
//    productButton.backgroundColor = rgb(233,84,18);
    [productButton setBackgroundImage:LBImage(@"wk_shadow_btn") forState:UIControlStateNormal];
    [productButton addTarget:self action:@selector(clickDingzhiBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //课程顾问会线下电话跟您联系，请保持手机畅通！
    UILabel *callLab = [UILabel labelWithText:@"课程顾问会线下电话跟您联系，请保持手机畅通！" andTextColor:rgb(101,101,101) andName:nil andFontSize:12*kScaleFit];
    [footerView addSubview:callLab];
    [callLab makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(productButton.mas_bottom).mas_equalTo(24*kScaleFit);
        make.centerX.mas_equalTo(footerView);
    }];
    //查看是否是vip
    [self requestDataIsvip];
    
}
- (void)requestDataIsvip{
    NSString *token = [UserModel getUserModel].token;
    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:WKMeUserInfo withPostParams:@{@"token":token == nil ? @"":token}.mutableCopy withBlock:^(NSDictionary *result, NSError *error) {
        NSString *code = result[@"code"];
        
        if ([code integerValue]==200) {
            VKPersonInfoModel *model = [VKPersonInfoModel mj_objectWithKeyValues:result[@"data"]];
            self.personInfoModel = model;
        }else if ([code integerValue]==401){
            
        }
        [self.tableView reloadData];
    }];
}
- (void)clickDingzhiBtn:(UIButton *)sender{
    if (self.ageString.length==0) {
        [MBProgressHUD showSuccess:@"请选择年龄" toView:self.view];
        return;
    }
    if (self.monthString.length==0) {
        [MBProgressHUD showSuccess:@"请选择月份" toView:self.view];
        return;
    }
    if (self.timeLongString.length==0) {
        [MBProgressHUD showSuccess:@"请选择时长" toView:self.view];
        return;
    }
    if (self.tel.length==0) {
        [MBProgressHUD showSuccess:@"请输入手机号" toView:self.view];
        return;
    }

    if (![self.tel isMobile]) {
        [MBProgressHUD showSuccess:@"请输入正确的手机号" toView:self.view];
        return;
    }
   
    if ([self.personInfoModel.isVip integerValue]==1) {
        NSMutableDictionary *data = [NSMutableDictionary dictionary];
        NSString *token = [UserModel getUserModel].token;
        [data setObject:token forKey:@"token"];
        [data setObject:self.ageString forKey:@"ageYear"];
        [data setObject:self.monthString forKey:@"ageMonth"];
        [data setObject:self.timeLongString forKey:@"courseLength"];
        [data setObject:self.tel forKey:@"tel"];
        [data setObject:@"2" forKey:@"planType"];
        
        [WYHXNetWorkManager post:WKQuerycommitPlanByVip_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                self.dataArray = [WKOnlinePingguResultModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
                
                LBVipEvaluationResultVC *vc = [[LBVipEvaluationResultVC alloc] init];
                vc.dataArray = self.dataArray;
                [self.navigationController pushViewController:vc animated:YES];

            }
        }];
    }else{
        [WYHXNetWorkManager get:WKQqueryExclusivePlanPrice_WKURL params:nil completionHandler:^(id responseObj, NSError *error) {
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                NSString *price = responseObj[@"data"];
                NSString *titleString = @"专属定制";
                WKOnlinePayVC *payVC = [WKOnlinePayVC new];
                payVC.titleString = titleString;
                payVC.price = [NSString stringWithFormat:@"%@",price];
                payVC.planType = @"2";
                payVC.ageMonth = self.monthString;
                payVC.ageYear = self.ageString;
                payVC.tel = self.tel;
                payVC.courseLength = self.timeLongString;
                [self.navigationController pushViewController:payVC animated:YES];
            }
        }];
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"Cell";
    LBZaiLineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[LBZaiLineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = self.titleArr[indexPath.section];
    cell.textField.delegate = self;
    cell.textField.tag = indexPath.section;
    switch (indexPath.section) {
        case 0:
        {
            cell.isNeed = YES;
            cell.isNext = YES;
            cell.textField.placeholder = @"请选择";
            //            cell.textField.text = self.infoModel.genderStr;
        }
            break;
        case 1:
        {
            cell.isNeed = YES;
            cell.isNext = YES;
            cell.textField.placeholder = @"请选择";
            //            cell.textField.text = self.infoModel.birthdayStr;
        }
            break;
        case 2:
        {
            cell.isNeed = NO;
            cell.isNext = YES;
            cell.textField.placeholder = @"请选择";
            //            cell.textField.text = self.infoModel.birthtimeStr;
        }
            break;
        case 3:
        {
            cell.isNeed = YES;
            cell.isNext = NO;
            cell.textField.placeholder = @"请输入";
            cell.textField.keyboardType = UIKeyboardTypeNumberPad;

            [cell.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        }
            break;
            
        default:
            break;
    }
    
    return cell;
    
}
#pragma mark - 检测手机号输入框
- (void)textFieldDidChange:(UITextField *)textField {
    self.tel = textField.text;
}

#pragma mark - UITextFieldDelegate 返回键
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
        if (textField.tag == 3) {
            [textField resignFirstResponder];
        }
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"%@",textField.text);
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"%@",textField.text);
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 3) {
        [textField addTarget:self action:@selector(handlerTextFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];
        return YES; // 当前 textField 可以编辑
    } else {
        [self.view endEditing:YES];
        [self handlerTextFieldSelect:textField];
        return NO; // 当前 textField 不可编辑，可以响应点击事件
    }
}
#pragma mark - 处理编辑事件
- (void)handlerTextFieldEndEdit:(UITextField *)textField {
//    NSLog(@"结束编辑:%@", textField.text);
    
    switch (textField.tag) {
        case 3:
        {
            self.tel = textField.text;
        }
            break;
        case 4:
        {
            
        }
            break;
            
        default:
            break;
    }
}

- (void)handlerTextFieldSelect:(UITextField *)textField {
    switch (textField.tag) {
        case 0:
        {
            [BRStringPickerView showStringPickerWithTitle:@"孩子年龄" dataSource:@[@"3岁", @"4岁", @"5岁",@"6岁", @"7岁", @"8岁"] defaultSelValue:textField.text resultBlock:^(id selectValue) {
                textField.text = selectValue;
                 self.ageString = [selectValue stringByReplacingOccurrencesOfString:@"岁" withString:@""];
            }];
        }
            break;
        case 1:
        {
            [BRStringPickerView showStringPickerWithTitle:@"孩子月份" dataSource:@[@"1月", @"2月", @"3月",@"4月", @"5月", @"6月",@"7月", @"8月", @"9月",@"10月",@"11月",@"12月"] defaultSelValue:textField.text resultBlock:^(id selectValue) {
                textField.text = selectValue;
                self.monthString = [selectValue stringByReplacingOccurrencesOfString:@"月" withString:@""];
            }];
        }
            break;
        case 2:
        {
            [BRStringPickerView showStringPickerWithTitle:@"学习时长" dataSource:@[@"1小时", @"2小时", @"3小时",@"4小时", @"5小时", @"6小时",@"7小时", @"8小时"] defaultSelValue:textField.text resultBlock:^(id selectValue) {
                textField.text = selectValue;
                self.timeLongString = [selectValue stringByReplacingOccurrencesOfString:@"小时" withString:@""];
            }];
        }
            break;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [UIView new];
    headerView.backgroundColor = rgb(242,242,242);
    return headerView;
}
@end
